package client.ui;

import java.util.List;

import phasebook.client.msgs.UserProfileInfo;
import client.Controller;
import client.Controller.ResponseHandler;
import client.api.CombinedResponse;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.layout.SectionStackSection;

public class ProfileSection extends SectionStackSection implements
		ResponseHandler {

	private Label name;
	private Label email;
	private Label balance;
	private UserProfileInfo user;
	private boolean isHome;

	public ProfileSection(final Controller controller, UserProfileInfo user, boolean isHome) {
		super("Profile");
		super.setExpanded(true);
		super.setResizeable(false);
		this.isHome = isHome;

		HLayout layout = new HLayout();
		layout.setWidth100();
		layout.setHeight("1%");
		layout.setLayoutMargin(6);

		name = new Label();
		name.setAlign(Alignment.LEFT);
		name.setValign(VerticalAlignment.CENTER);
		name.setWrap(false);
		name.setShowEdges(false);

		email = new Label();
		email.setAlign(Alignment.LEFT);
		email.setValign(VerticalAlignment.CENTER);
		email.setWrap(false);
		email.setShowEdges(false);

		balance = new Label();
		balance.setAlign(Alignment.RIGHT);
		balance.setValign(VerticalAlignment.CENTER);
		balance.setWrap(false);
		balance.setShowEdges(false);

		setUser(user);

		layout.addMember(name);
		layout.addMember(new LayoutSpacer());
		layout.addMember(email);
		layout.addMember(new LayoutSpacer());
		layout.addMember(balance);

		super.addItem(layout);
		
		controller.addResponseHandler(this);

		
	}

	public void setUser(UserProfileInfo user) {
		name.setContents("<b>" + user.getName() + "</b> (<i>"
				+ user.getUsername() + "</i>)");
		email.setContents("<i>" + user.getEmail() + "</i>");
		balance.setContents("<b>" + user.getBalance() + " L€</b>");
		this.user = user;
	}

	@Override
	public void onResponse(CombinedResponse response) {
		List<UserProfileInfo> profiles = response.getProfiles();
		if (profiles != null)
			for (UserProfileInfo p : profiles)
				if (p.getUserId() == user.getUserId())
					setUser(p);
	}

}
